<?php 
namespace town\filter;

use town\base\Filter;
use town\base\Request;
use town\base\App;

class Auth implements Filter
{

    /**
     * 登录验证,仅仅后台验证需要
     * @param  Request  $request 请求对象
     * @param  callable $next    链
     * @return 响应对象
     */
    public function doFilter(Request $request, callable $next)
    {
        $isLogin= ($_SESSION['userid'] > 0);
        $route  = (App::singleton()->route);
        $isView = ($route['local'] == 'peim3');
        $isAct  = ($route['local'] == 'login');

        // 1.登录了,访问的是(peim3|login)  -> '/back'
        if ($isLogin && ($isView || $isAct)) {
            return redirect('/back');
        }

        // 2.没有登录,访问的不是(peim3|login)  -> '/peim3' 
        if (!$isLogin && !$isView && !$isAct) {
            return redirect('/peim3');
        }

        return $next($request);
    }
}